package com.tech.tool.controller;


import com.tech.tool.dataobject.ProjectInfo;
import com.tech.tool.dto.projectInfo.SelectProjectListDTO;
import com.tech.tool.service.ProjectInfoService;
import com.tech.tool.utils.ListVOUtil;
import com.tech.tool.utils.ResultVOUtil;
import com.tech.tool.vo.ListVO;
import com.tech.tool.vo.ResultVO;
import com.tech.tool.vo.projectInfo.SelectProjectListVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/api/project")
public class ProjectInfoController {

    @Autowired
    private ProjectInfoService projectInfoService;

    @PostMapping(path = "/selectProjectList",
            consumes = "application/json;charset=UTF-8", produces = "application/json;charset=UTF-8")
    @CrossOrigin(origins = "*", maxAge = 3600)
    public ResultVO selectProjectList(@RequestBody SelectProjectListDTO selectProjectListDTO) {

        Page<ProjectInfo> projectInfoPage = projectInfoService.findByBu(selectProjectListDTO.getBuId());
        List<SelectProjectListVO> selectProjectListVOList = new ArrayList<>();

        for (ProjectInfo projectInfo: projectInfoPage.getContent()) {
            SelectProjectListVO selectProjectListVO = new SelectProjectListVO();
            BeanUtils.copyProperties(projectInfo, selectProjectListVO);
            selectProjectListVOList.add(selectProjectListVO);
        }

        ListVO listVO = new ListVOUtil().setPageInfo(projectInfoPage, selectProjectListVOList);
        listVO.setList(selectProjectListVOList);

        return ResultVOUtil.success(listVO);

    }

}
